package com.zone.gateway.core.socket.handler;

import com.zone.gateway.core.session.Configuration;
import com.zone.gateway.core.socket.BaseHandler;
import com.zone.gateway.core.socket.agreement.AgreementConstant;
import com.zone.gateway.core.mapping.HttpStatement;
import com.zone.gateway.core.socket.agreement.RequestParserDirector;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: zongzi
 * @description: session channel 自定义处理器，进行http - rpc协议转换
 * @date: 2025/4/17
 */
public class GatewayChannelHandler extends BaseHandler<FullHttpRequest> {
    private final Logger logger = LoggerFactory.getLogger(GatewayChannelHandler.class);

    private final Configuration configuration;

    public GatewayChannelHandler(Configuration configuration) {
        this.configuration = configuration;
    }


    @Override
    protected void session(ChannelHandlerContext ctx, Channel channel, FullHttpRequest request) {
        RequestParserDirector requestParserDirector = new RequestParserDirector(request);

        // 解析uri
        String uri = requestParserDirector.getUri();
        HttpStatement httpStatement = configuration.getHttpStatement(uri);
        // 将HTTP statement存入channel中
        channel.attr(AgreementConstant.HTTP_STATEMENT).set(httpStatement);

        // 传递给下一个handler
        request.retain();
        ctx.fireChannelRead(request);
    }
}
